<?php

/*
	快速执行sql
*/

	class sql {		

		function total($biao,$where){
	        $result = $this->custom("SELECT count(id) as total FROM ".$biao." where ".$where,true);
	        return $result[0]['total'];
	    }

		function total_custom($sql){
	        $result = pdd_query($sql);
	        return pdd_num_rows($result);
	    }
		
		//执行SQL文件内容
		function multi($data){
			return pdd_multi_query($data);
		}
		
		function isTable($tableName){
			$result = pdd_query("DESCRIBE user_soft_user");
			if(!$result)
			{
				return false;
			}
			$existingFields = array();
			while ($row = $result->fetch_assoc()) {
				$existingFields[] = $row['Field'];
			}
			return $existingFields;
		}

		function lock($biao)
		{
			pdd_query("lock tables ".$biao." WRITE");
		}
		function unlock()
		{
			pdd_query("unlock tables");
		}
		
		function read_all($biao,$where)
		{
			$data = array();
			$result = pdd_query("SELECT * FROM ".$biao." where ".$where);
			while($row = pdd_fetch_array($result))
			{
				foreach ($row as $key => $value)
					{

						if(!is_numeric($key))
						{
							// 判断是否为数值类型
							if(is_numeric($value)) {
								// 如果是整数
								if(ctype_digit($value)) {
									$str[$key] = intval($value);
								} else {
									// 如果是浮点数
									$str[$key] = floatval($value);
								}
							} else {
								// 文本类型保持不变
								$str[$key] = $value;
							}
						}
					}//获取所有字段信息
				array_push($data,$str);//插入到数组
			}
			return $data;
		}
		//读取指定表的指定数量数据
		function read($biao,$page,$limit,$where,&$return_total)
		{
			$s_index = (intval($page)-1) * $limit;
			$data = $this->custom("SELECT * FROM ".$biao." where $where limit $s_index,$limit",true);
			$ret = $this->custom("SELECT COUNT(id) AS total FROM $biao WHERE $where",true);
    		$return_total = intval(str_replace(",","",$ret[0]['total']));
			return $data;
		}

		function custom($sql,$isselect = true)
		{
			$result = pdd_query($sql);
			if($isselect)
			{
				$data = array();
				while($row = pdd_fetch_array($result))
				{
					foreach ($row as $key => $value)
					{
						if(!is_numeric($key))
						{
							// 判断是否为数值类型
							if(is_numeric($value)) {
								// 如果是整数
								if(ctype_digit($value)) {
									$str[$key] = intval($value);
								} else {
									// 如果是浮点数
									$str[$key] = floatval($value);
								}
							} else {
								// 文本类型保持不变
								$str[$key] = $value;
							}
						}
					}//获取所有字段信息
					array_push($data,$str);//插入到数组
				}
				return $data;
			}
			
			return $result;
		}
		//按条件只读取一条数据
		function read_only($biao,$where)
		{
		
			$result = pdd_query("SELECT * FROM ".$biao." where ".$where);
			if($row = pdd_fetch_array($result))
			{
				foreach ($row as $key => $value)
				{
					if(!is_numeric($key))
						{
							// 判断是否为数值类型
							if(is_numeric($value)) {
								// 如果是整数
								if(ctype_digit($value)) {
									$str[$key] = intval($value);
								} else {
									// 如果是浮点数
									$str[$key] = floatval($value);
								}
							} else {
								// 文本类型保持不变
								$str[$key] = $value;
							}
						}
				}//获取所有字段信息
				return $str;	
			}
			return null;
		}

	//模糊匹配数据：按字段内容包含源内容查找方式
		function read_like($biao,$ziduan,$str)
		{
			$result = pdd_query("SELECT * FROM ".$biao);
			if($row = pdd_fetch_array($result))
			{
				$tmp = $row[$ziduan];

				if(strpos($str,$tmp)!==false)
				{
					foreach ($row as $key => $value)
					{
						if(!is_numeric($key))
						{
						 	$str[$key] = urlencode($value);
						}
					}//获取所有字段信息
					return $str;	
				}

				
			}
			return null;
		}



		//json 键名作为字段名插入到指定数据表
		function insert_json($biao,$json)
		{
			foreach ($json as $key => $value) {
			$sql1 = $sql1.$key.",";
			$sql2 = $sql2."'".$value."',";
			}
			$sql1 = substr($sql1,0,strlen($sql1)-1);
			$sql2 = substr($sql2,0,strlen($sql2)-1);
			//echo "INSERT INTO ".$biao." (".$sql1.")values(".$sql2.");";
			#$f = fopen("log.txt", "a+");
			#fwrite($f, "INSERT INTO ".$biao." (".$sql1.")values(".$sql2.");"."\r\n");
			#fclose($f);
			pdd_query("INSERT INTO ".$biao." (".$sql1.")values(".$sql2.");");
			return pdd_insert_id();

		}

		//json 键名作为字段名更新到指定数据表
		function update_json($biao,$json,$where)
		{
			foreach ($json as $key => $value) {
			$sql = $sql.$key."='".$value."',";
			}
			$sql = substr($sql,0,strlen($sql)-1);
			//echo "UPDATE ".$biao." SET ".$sql." where ".$where;
			#$f = fopen("log.txt", "a+");
			#fwrite($f,  "UPDATE ".$biao." SET ".$sql." where ".$where."\r\n");
			#fclose($f);
			$result = pdd_affected_rows(pdd_query("UPDATE ".$biao." SET ".$sql." where ".$where));
			return $result;
		}

		//不同条件的数据更新，用法（json键名条件）  arr[0] = "id=1" ； arr[1] = "id=2"
		function update_json_json($biao,$json,$where_json)
		{
			foreach ($json as $key => $value) {
				$sql = $sql.$key."='".$value."',";
			}
			$sql = substr($sql,0,strlen($sql)-1);


			//数组json。。。
			for ($i=0;$i<count($where_json);$i++)
			{
				foreach ($where_json[$i] as $key => $value) {
					$where = $where . $key."='".$value."' and";
				}
				$where = substr($where,0,strlen($where)-3)."or ";
			}
			$where = substr($where,0,strlen($where)-3);
			
			pdd_query("UPDATE ".$biao." SET ".$sql." where ".$where);
		}

		//不同条件的数据更新，用法（数组文本条件）  arr[0] = "id=1" ； arr[1] = "id=2"
		function update_json_arr($biao,$json,$where_arr)
		{
			foreach ($json as $key => $value) {
			$sql = $sql.$key."='".$value."',";
			}
			$sql = substr($sql,0,strlen($sql)-1);

			for($i=0;$i<count($where_arr);$i++)
			{
				$where = $where . $where_arr[$i]." or";
			}
			$where = substr($where,0,strlen($where)-2);
			pdd_query("UPDATE ".$biao." SET ".$sql." where ".$where);
		}


		function delete($biao,$where)
		{
			pdd_query("DELETE FROM ".$biao." where ".$where);
		}

		function delete_arr($biao,$sql,$where_json)
		{
				//数组json。。。
			for ($i=0;$i<count($where_json);$i++)
			{
				foreach ($where_json[$i] as $key => $value) {
					$where = $where . $key."='".$value."' and";
				}
				$where = substr($where,0,strlen($where)-3)."or ";
			}
			$where = substr($where,0,strlen($where)-3);
			
			pdd_query("DELETE FROM ".$biao." where ".$sql." and ".$where);
		}

    		
	}


?>